package com.xiaohui.tool;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;

/**
 *
 * @author xiaohui
 * 线程池优雅关闭
 */
@Slf4j
public class ThreadPoolUtils {
    private ThreadPoolUtils() {

    }

    public static void shutDown(ExecutorService pool, int shutDownTime, int shutDownNowTime, TimeUnit timeUnit) {
        pool.shutdown();
        try {
            if (!pool.awaitTermination(shutDownTime, timeUnit)) {
                pool.shutdownNow();
                if (!pool.awaitTermination(shutDownNowTime, timeUnit)) {
                    log.error("线程池关闭失败");
                }
            }
        } catch (InterruptedException e) {
            // 关闭线程被打断
            pool.shutdownNow();
            Thread.currentThread().interrupt();
        }

    }
}
